package com.sgcc.ln.pmos.e_ln_order.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.ln.pmos.common.utils.BigDecimalUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_order.bean.KeyValueBean;

/**
 * 合约
 */
public class DaypartingEleChangesAdapter extends BaseQuickAdapter<KeyValueBean, BaseViewHolder> {

    private Context mContext;
    private OnEditCallBack onEditCallBack;

    public DaypartingEleChangesAdapter() {
        super(R.layout.item_daypartingelechanges);
    }


    public void setOnEditCallBack(OnEditCallBack onEditInputCallBack) {
        this.onEditCallBack = onEditInputCallBack;
    }

    public interface OnEditCallBack {
        void onCurve(int position);


    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, KeyValueBean bean) {
        baseViewHolder.setText(R.id.tv_time, bean.getKey());
        baseViewHolder.setText(R.id.et_input_time, BigDecimalUtils.onKeep3Decimal(bean.getValue()));

        TextView tvOldValue = baseViewHolder.getView(R.id.tvOldValue);
        tvOldValue.setVisibility(bean.isShowOldValue() ? View.VISIBLE : View.INVISIBLE);
        SpanUtils.with(tvOldValue).append(BigDecimalUtils.onKeep3Decimal(bean.getOldValue()))
                .setStrikethrough().create();
        EditText editText = baseViewHolder.getView(R.id.et_input_time);
        editText.setTextColor(Color.parseColor(bean.isShowOldValue() ? "#FF0000" : "#333333"));


        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = editText.getText().toString().trim();
                bean.setValue(content);
            }
        };

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    editText.addTextChangedListener(textWatcher);
                } else {
                    editText.removeTextChangedListener(textWatcher);
                    bean.setShowOldValue(!bean.getValue().equals(bean.getOldValue()));
                    baseViewHolder.setText(R.id.et_input_time, BigDecimalUtils.onKeep3Decimal(bean.getValue()));
                    tvOldValue.setVisibility(bean.isShowOldValue() ? View.VISIBLE : View.INVISIBLE);
                    SpanUtils.with(tvOldValue).append(BigDecimalUtils.onKeep3Decimal(bean.getOldValue()))
                            .setStrikethrough().create();
                    editText.setTextColor(Color.parseColor(bean.isShowOldValue() ? "#FF0000" : "#333333"));
                    onEditCallBack.onCurve(getItemPosition(bean));
                }
            }
        });


    }


}
